package com.buildings.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.buildings.R;
import com.buildings.bean.VersionBean;

import java.io.File;

public class UpdataFragmentDialog extends DialogFragment implements View.OnClickListener {
    private TextView tv_igno, tv_yes, tv_desc, tv_version;
    private Activity activity;

    public static UpdataFragmentDialog newInstance(VersionBean.DataDTO updateInfo) {
        UpdataFragmentDialog updataFragmentDialog = new UpdataFragmentDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("updateInfo", updateInfo);
        updataFragmentDialog.setArguments(bundle);
        return updataFragmentDialog;

    }

    private VersionBean.DataDTO updateInfo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.custom_fragment_dialog);
        updateInfo = (VersionBean.DataDTO) getArguments().getSerializable("updateInfo");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    private View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.dialog_select_version_type, container, false);
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        if (updateInfo != null && updateInfo.getIsForceUpdate().equals("1")) {
            getDialog().setCancelable(false);
            getDialog().setCanceledOnTouchOutside(false);
            getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
        activity =  getActivity();
        initView();
        initData();
        return mView;
    }

    private File file;

    private void initData() {
        tv_igno.setVisibility(View.VISIBLE);
        if (updateInfo == null) {
            return;
        }
        file = null;
        tv_desc.setText(updateInfo.getMessage());
        tv_version.setText("V" + updateInfo.getVersionName());
        if (updateInfo.getIsForceUpdate().equals("1")) {
            tv_igno.setVisibility(View.GONE);
        } else {
            tv_igno.setVisibility(View.VISIBLE);
        }
        if (null != file && file.exists()) {
            tv_yes.setText("立即安装");
        } else {
            tv_yes.setText("立即下载");
        }
    }

    private void initView() {
        tv_igno = mView.findViewById(R.id.tv_igno);
        tv_yes = mView.findViewById(R.id.tv_yes);
        tv_desc = mView.findViewById(R.id.tv_desc);
        tv_version = mView.findViewById(R.id.tv_version);
        tv_yes.setOnClickListener(this);
        tv_igno.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_yes:
                if (updateInfo == null) {
                    return;
                }
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(updateInfo.getUrl());
                intent.setData(content_url);
                startActivity(intent);
//                if (file != null && file.exists()) {
//                    //已下载
//                    Uri uri = HYAppUtils.getProviderUril(activity, file);
////                    Uri uri = FileProvider.getUriForFile(activity, "com.imohoo.njgh.integration.fileprovider", file);
//                    if (null != uri) {
//                        HYAppUtils.promptInstall(activity, uri);
//                    }
//                    if (updateInfo.getIsForceUpdate() != 1) {
//                        dismiss();
//                    }
//                    return;
//                }
//                UpdateUtils.startDownload(activity, updateInfo);
                break;
            case R.id.tv_igno:
                dismiss();
                break;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (updateInfo != null && updateInfo.getIsForceUpdate().equals("1")) {
            tv_yes.setText("立即安装");
        }
    }
}
